﻿using System.IO;
using FortuneUniverse.Fortune.Algorithms;

namespace FortuneUniverse.Fortune.Dyson
{
  public struct VeinData
  {
    public int id;
    public EVeinType type;
    public short modelIndex;
    public short groupIndex;
    public int amount;
    public int productId;
    public Vector3 pos;
    public int minerCount;
    public int minerId0;
    public int minerId1;
    public int minerId2;
    public int minerId3;
    public int modelId;
    public int colliderId;
    public int minerBaseModelId;
    public int minerCircleModelId0;
    public int minerCircleModelId1;
    public int minerCircleModelId2;
    public int minerCircleModelId3;
    public static float oilSpeedMultiplier = 4E-05f;

    public void SetNull()
    {
      this.id = 0;
      this.type = EVeinType.None;
      this.modelIndex = (short) 0;
      this.groupIndex = (short) 0;
      this.amount = 0;
      this.productId = 0;
      this.pos.x = 0.0f;
      this.pos.y = 0.0f;
      this.pos.z = 0.0f;
      this.minerCount = 0;
      this.minerId0 = 0;
      this.minerId1 = 0;
      this.minerId2 = 0;
      this.minerId3 = 0;
      this.modelId = 0;
      this.colliderId = 0;
      this.minerBaseModelId = 0;
      this.minerCircleModelId0 = 0;
      this.minerCircleModelId1 = 0;
      this.minerCircleModelId2 = 0;
      this.minerCircleModelId3 = 0;
    }

    public void Export(BinaryWriter w)
    {
      w.Write(0);
      w.Write(this.id);
      w.Write((short) this.type);
      w.Write(this.modelIndex);
      w.Write(this.groupIndex);
      w.Write(this.amount);
      w.Write(this.productId);
      w.Write(this.pos.x);
      w.Write(this.pos.y);
      w.Write(this.pos.z);
      w.Write(this.minerCount);
      w.Write(this.minerId0);
      w.Write(this.minerId1);
      w.Write(this.minerId2);
      w.Write(this.minerId3);
    }

    public void Import(BinaryReader r)
    {
      int num = (int) r.ReadByte();
      this.id = r.ReadInt32();
      this.type = (EVeinType) r.ReadInt16();
      this.modelIndex = r.ReadInt16();
      this.groupIndex = r.ReadInt16();
      this.amount = r.ReadInt32();
      this.productId = r.ReadInt32();
      this.pos.x = r.ReadSingle();
      this.pos.y = r.ReadSingle();
      this.pos.z = r.ReadSingle();
      this.minerCount = r.ReadInt32();
      this.minerId0 = r.ReadInt32();
      this.minerId1 = r.ReadInt32();
      this.minerId2 = r.ReadInt32();
      this.minerId3 = r.ReadInt32();
    }

    public void AddMiner(int minerId)
    {
      if (this.minerId0 == minerId || this.minerId1 == minerId || this.minerId2 == minerId || this.minerId3 == minerId)
        return;
      if (this.minerId0 == 0)
      {
        this.minerId0 = minerId;
        ++this.minerCount;
      }
      else if (this.minerId1 == 0)
      {
        this.minerId1 = minerId;
        ++this.minerCount;
      }
      else if (this.minerId2 == 0)
      {
        this.minerId2 = minerId;
        ++this.minerCount;
      }
      else
      {
        if (this.minerId3 != 0)
          return;
        this.minerId3 = minerId;
        ++this.minerCount;
      }
    }

    public void RemoveMiner(int minerId)
    {
      if (this.minerId0 == minerId)
      {
        this.minerId0 = 0;
        --this.minerCount;
        this.minerId0 = this.minerId1;
        this.minerId1 = this.minerId2;
        this.minerId2 = this.minerId3;
        this.minerId3 = 0;
      }
      else if (this.minerId1 == minerId)
      {
        this.minerId1 = 0;
        --this.minerCount;
        this.minerId1 = this.minerId2;
        this.minerId2 = this.minerId3;
        this.minerId3 = 0;
      }
      else if (this.minerId2 == minerId)
      {
        this.minerId2 = 0;
        --this.minerCount;
        this.minerId2 = this.minerId3;
        this.minerId3 = 0;
      }
      else
      {
        if (this.minerId3 != minerId)
          return;
        this.minerId3 = 0;
        --this.minerCount;
      }
    }
  }
}
